package com.komodo.island.gdx;

import com.badlogic.gdx.InputProcessor;
import com.komodo.island.gdx.dialog.Dialog;
import com.komodo.island.gdx.screen.GameScreen;
import com.komodo.island.gdx.screen.MainMenuScreen;
import com.komodo.island.gdx.screen.MapScreen;
import com.komodo.island.gdx.screen.Screen;

public class Input implements InputProcessor {

	/*
	 * This will handle all input for this game. This will handle input by its
	 * current state whether when the screen is running or the dialog is
	 * showing.
	 * 
	 * It will handle all screen input or dialog input, just register new screen
	 * or new dialog here and add "new" case in keyUp or touchUp method or the
	 * others.
	 */

	public static final int DIALOG_PAUSE = 1;
	public static final int DIALOG_WIN = 2;
	public static final int DIALOG_LOSE = 3;
	public static final int DIALOG_EXIT = 4;
	public static final int DIALOG_LOADING = 5;
	public static final int DIALOG_NULL = 999;
	public static final int DIALOG_MAIN_MENU = 6;
	public static final int DIALOG_PLAYER_SELECT = 7;
	public static final int DIALOG_TOURIST_MAP = 8;
	public static final int DIALOG_SCIENTIST_MAP = 9;
	

	public static final int DIALOG_SHOPPING = 10;
	public static final int DIALOG_SUIT = 11;
	public static final int DIALOG_UNDERWATER= 12;
	public static final int DIALOG_SAND= 13;
	
	

	public static final int SCREEN_SPLASH = 0;
	public static final int SCREEN_MAIN = 1;
	public static final int SCREEN_PLAYER = 2;
	public static final int SCREEN_SCIENTIST = 3;
	public static final int SCREEN_TOURIST = 4;
	public static final int SCREEN_WIKI = 5;
	public static final int SCREEN_GAME = 6;
	public static final int SCREEN_STORY = 7;
	public static final int DIALOG_GAME_DESC = 14;

	private KomodoIsland komodoIsland;
	public boolean dialogShow = false;
	public boolean swypeOn = false;

	private Dialog stage;

	public void init(KomodoIsland komodoIsland) {
		this.komodoIsland = komodoIsland;
	}

	public void setStage(Dialog stage) {
		this.stage = stage;
		dialogShow = true;
	}

	public void removeStage() {
		dialogShow = false;
	}

	public boolean keyDown(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		if ((arg0 == com.badlogic.gdx.Input.Keys.P)
				|| (arg0 == com.badlogic.gdx.Input.Keys.MENU)
				|| (arg0 == com.badlogic.gdx.Input.Keys.BACK)) {
			/*
			 * Handle Key pressed for P, Back, and Menu
			 */

			if (dialogShow) {
				/*
				 * If Dialog is shown, this will handle the input for the Dialog
				 */
				if (((Dialog) stage).ID == DIALOG_PAUSE) {
					if (((Screen) komodoIsland.getScreen()).ID == SCREEN_GAME) {
						((GameScreen) komodoIsland.getScreen()).resumeGame();
					} else {
						((Screen) komodoIsland.getScreen()).setScreenToRun();
					}

				} else if (stage.ID == DIALOG_MAIN_MENU) {
					((MainMenuScreen) komodoIsland.getScreen()).exitScreen();
				} else if (stage.ID == DIALOG_EXIT) {
					((Screen) komodoIsland.getScreen()).setScreenToRun();
				} else if (stage.ID == DIALOG_PLAYER_SELECT) {
					((Screen) komodoIsland.getScreen()).exitScreen();
				} else if (stage.ID == DIALOG_TOURIST_MAP) {
					((Screen) komodoIsland.getScreen()).exitScreen();
				} else if (stage.ID == DIALOG_SCIENTIST_MAP) {
					((Screen) komodoIsland.getScreen()).exitScreen();
				}else if (stage.ID == DIALOG_GAME_DESC){
					((MapScreen) komodoIsland.getScreen()).hideGameDesc();
				}

				else {
					if (((Screen) komodoIsland.getScreen()).ID == SCREEN_GAME) {
						((GameScreen) komodoIsland.getScreen()).pauseGame();
					}
				}

			} else {
				/*
				 * Handling the input when there is no dialog
				 */
				if (((Screen) komodoIsland.getScreen()).ID == SCREEN_GAME) {
					((GameScreen) komodoIsland.getScreen()).pauseGame();

				} else if (((Screen) komodoIsland.getScreen()).ID == SCREEN_MAIN) {

				} else {
					((Screen) komodoIsland.getScreen()).exitScreen();
				}
			}
//			System.out.println("Pause Key Pressed");
		}
		/*
		 * Handle Key Press for 1 = main menu 2 = player 3 = tourist 4 =
		 * scientist
		 */
//		else if (arg0 == com.badlogic.gdx.Input.Keys.NUM_1) {
//			// if (!dialogShow){
//			/*
//			 * Handling the input when there is no dialog
//			 */
//			komodoIsland.goToScreen(komodoIsland.mainMenuScreen);
//			// }
//		} else if (arg0 == com.badlogic.gdx.Input.Keys.NUM_2) {
//			// if (!dialogShow){
//			/*
//			 * Handling the input when there is no dialog
//			 */
//			komodoIsland.goToScreen(komodoIsland.playerScreen);
//			// }
//		} else if (arg0 == com.badlogic.gdx.Input.Keys.NUM_3) {
//			// if (!dialogShow){
//			/*
//			 * Handling the input when there is no dialog
//			 */
//			komodoIsland.goToScreen(komodoIsland.scientistScreen);
//			// }
//		} else if (arg0 == com.badlogic.gdx.Input.Keys.NUM_4) {
//			// if (!dialogShow){
//			/*
//			 * Handling the input when there is no dialog
//			 */
//			komodoIsland.gameSuit.startGame();
//			komodoIsland.goToScreen(komodoIsland.gameSuit);
//			// }
//		} else if (arg0 == com.badlogic.gdx.Input.Keys.NUM_5) {
//			// if (!dialogShow){
//			/*
//			 * Handling the input when there is no dialog
//			 */
//			komodoIsland.gameBalance.startGame();
//			komodoIsland.goToScreen(komodoIsland.gameBalance);
//			// }
//		} else if (arg0 == com.badlogic.gdx.Input.Keys.NUM_6) {
//			// if (!dialogShow){
//			/*
//			 * Handling the input when there is no dialog
//			 */
//			komodoIsland.gameKomodoPhoto.startGame();
//			komodoIsland.goToScreen(komodoIsland.gameKomodoPhoto);
//			// }
//		} else if (arg0 == com.badlogic.gdx.Input.Keys.NUM_7) {
//			// if (!dialogShow){
//			/*
//			 * Handling the input when there is no dialog
//			 */
////			komodoIsland.gameShopping.startGame();
////			komodoIsland.goToScreen(komodoIsland.gameShopping);
//			// }
//		}
//
//		else if (arg0 == com.badlogic.gdx.Input.Keys.E) {
//			if (!dialogShow) {
//				((Screen) komodoIsland.getScreen()).exitScreen();
//			} else {
//				if (((Screen) komodoIsland.getScreen()).ID == SCREEN_MAIN) {
//					if (stage.ID == DIALOG_MAIN_MENU) {
//						((MainMenuScreen) komodoIsland.getScreen())
//								.exitScreen();
//					} else if (stage.ID == DIALOG_EXIT) {
//						System.exit(0);
//					}
//
//				}
//			}
//
//		} else if (arg0 == com.badlogic.gdx.Input.Keys.Y) {
//			if (dialogShow) {
//				if (stage.ID == Input.DIALOG_EXIT) {
//					System.out.println("EXIT, bye2");
//					System.exit(0);
//				}
//			}
//		} else if (arg0 == com.badlogic.gdx.Input.Keys.N) {
//			if (dialogShow) {
//				if (stage.ID == Input.DIALOG_EXIT) {
//					((Screen) komodoIsland.getScreen()).setScreenToRun();
//				}
//			}
//		} else if (arg0 == com.badlogic.gdx.Input.Keys.H) {
////			komodoIsland.story1.nextScreen = ((Screen) komodoIsland.getScreen());
////			komodoIsland.story1.restartStory();
////			komodoIsland.goToScreen(komodoIsland.story1);
//		}
		

		return false;
	}

	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		if (dialogShow) {
			return stage.touchDown(arg0, arg1, arg2, arg3);
		}
		return false;
	}

	public boolean touchDragged(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean touchMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		if (dialogShow) {
			// System.out.println("Stage Touched at "+arg0+","+arg1);
			return stage.touchUp(arg0, arg1, arg2, arg3);
		} else {
//			if (komodoIsland.getScreen() instanceof SuitKomodo) {
//
//			}
			// System.out.println(komodoIsland.getScreen());
		}
		// System.out.println("Touched at "+arg0+","+arg1);
		return false;
	}

}
